#-*- coding: UTF-8 -*-

import sys
import json

from urllib import urlretrieve
import urllib2
from lxml import etree


config = None


def get_issue_info():
    global config
    url = config['issue_home']
    print("get issue update's info : " + url)

    send_headers = {
        'Host': 'fullcirclemagazine.org',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; rv:16.0) Gecko/20100101 Firefox/16.0',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Connection': 'keep-alive'
    }

    req = urllib2.Request(url, headers=send_headers)
    f = urllib2.urlopen(req)
    data = f.read()
    print(len(data))
    return data


def extract_book_home_url(html_src):
    print(r'extracting book home urls.......')
    document = etree.HTML(html_src)
    book_home = document.xpath(u"//table[@class='issuetable']/tr/td/a/@href")
    print(len(book_home))
    return book_home


def extract_book_url(home_urls, ext):
    global config
    base_url = config['download_base']
    print(r'extracting book download url....')
    book_urls = []
    for index, bh in enumerate(home_urls):
        url = base_url + str(index+1) + '_en.'+ext
        book_urls.append(url)
    print(book_urls)
    return book_urls


def report(count, blockSize, totalSize):
    percent = int(count*blockSize*100/totalSize)
    sys.stdout.write(u"\r process : %d%%" % percent + ' complete')
    sys.stdout.flush()


def download_book(url):
    global config
    print(u'\ncurrent: %s' % url)
    file_name = url.rsplit('/', 1)
    print(r'downloading book : ' + file_name[1])
    urlretrieve(url, config['local_path'] + file_name[1], reporthook=report)
    config['start_id'] += 1


def start_download(urls, start_id):
    print(r'start downloading..........')
    for url in urls[start_id:]:
        download_book(url)
        pass


def load_config():
    global config
    with open('config.json', 'r') as f:
        config = json.load(f, encoding='utf-8')
    print(config)


def save_config():
    global config
    with open('config.json', 'wb') as f:
        config = json.dump(config, f)


def get_start_id(max_len):
    global config
    while True:
        start_id = raw_input(r'please enter the start id [default %d]: ' % config['start_id'])
        if start_id.isdigit():
            start_id_int = int(float(start_id))
            if start_id_int > max_len:
                print('please input integer: [1 -- %d]' % max_len)
            else:
                break
        elif start_id.__eq__(''):
            return config['start_id']
        else:
            print('please input integer: [1 -- %d]' % max_len)
    print(r'accepted id: %d' % start_id_int)
    return start_id_int - 1


if __name__ == '__main__':
    load_config()
    html = get_issue_info()
    book_home_urls = extract_book_home_url(html)
    book_download_urls = extract_book_url(book_home_urls, "pdf")

    print(r'found total book: %d' % len(book_download_urls))
    start_download(book_download_urls, get_start_id(len(book_download_urls)))
    save_config()
